package com.liu.nb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.liu.nb.entity.Debugtask;
import com.liu.nb.entity.DebugtaskUser;
import com.liu.nb.entity.Patroltask;
import com.liu.nb.entity.SysUser;
import com.liu.nb.service.IDebugtaskService;
import com.liu.nb.util.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 消缺任务和用户中间表 前端控制器
 * </p>
 *
 * @author liuziyi
 * @since 2021-08-31
 */
@RestController
@RequestMapping("/nb/debugtask-user")
public class DebugtaskUserController extends BaseController{

    /**
     * 加载消缺员列表
     * @return
     */
    @PreAuthorize("hasRole('xianluadmin')")
    @GetMapping("/getDefectUser")
    public Result getDefectUser(){
        List<SysUser> inspectUserList = userService.list(new QueryWrapper<SysUser>()
                .like("username", "xiaoque"));
        return Result.success(inspectUserList);
    }
    /**
     * 加载负责人列表（管理员）
     * @return
     */
    @PreAuthorize("hasRole('xianluadmin')")
    @GetMapping("/getPrincipal")
    public Result getPrincipal(){
        List<SysUser> inspectUserList = userService.list(new QueryWrapper<SysUser>()
                .like("username", "admin"));
        return Result.success(inspectUserList);
    }

    /**
     * 分配消缺任务
     * @param debugtaskUserList
     * @return
     */
    @PreAuthorize("hasRole('xianluadmin')")
    @PostMapping("/saveDeBugTaskUser")
    public Result saveDeBugTaskUser(List<DebugtaskUser> debugtaskUserList){
        String did = debugtaskUserList.get(0).getDid();
        Debugtask debugtask = new Debugtask();
        debugtask.setId(did);
        debugtask.setState(1);
        boolean b = debugtaskUserService.saveBatch(debugtaskUserList);
        boolean b1 = true;
        if(b){
            b1 = debugtaskService.updateById(debugtask);
        }else {
            b1 = false;
        }

        return Result.success(b1);
    }

    /**
     * 线路管理员取消消缺任务，不是删除
     * @param debugtask
     * @return
     */
    @PreAuthorize("hasRole('xianluadmin')")
    @PostMapping("/cancelTask")
    public Result cancelTask(Debugtask debugtask) {
        debugtask.setCancel(0);
        boolean b = debugtaskService.updateById(debugtask);
        return Result.success(b);
    }
}
